﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// DraftList 的摘要说明。
    /// </summary>
    public partial class DraftListPage : Page
    {
        private int _activeNodeId;
        private string _userName;

        protected bool ManageFlow
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            dgDraftList.SelectedIndexChanged += dgDraftList_SelectedIndexChanged;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            _userName = SiteContext.Current.UserName;

            // Get the id of the active node
            _activeNodeId = SiteContext.Current.ActiveNodeID;
            ManageFlow = AssignRights.IsAccessAllowed(_userName, _activeNodeId, PermissionType.ManageFlow);

            //得到用户名
            if (!Page.IsPostBack)
            {
                Bangding();
            }

            base.OnLoad(e);
        }

        public string GetDisplayDocumentUrl(int DocID)
        {
            return "NewDocument.aspx?DocID=" + DocID;
        }

        public string GetDisplayStepMemberUrl(int DocID)
        {
            return "DisplayTacheMember.aspx?DocID=" + DocID;
        }

        private void dgDraftList_SelectedIndexChanged(object sender, EventArgs e)
        { }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgDraftList.CurrentPageIndex = e.NewPageIndex;
            Bangding();
        }

        #endregion

        #region 绑定DBGRID

        private void Bangding()
        {
            var mySQL = new Database();
            SqlParameter[] parameters = {
                Database.MakeInParam("@StaffName", SqlDbType.NVarChar, 50, _userName)
            };

            DataTable dt;
            using (var dr = mySQL.RunDataReader("dbo.sproc_Flow_GetMyDraft_V2", parameters))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }

            dgDraftList.DataSource = dt.DefaultView;
            dgDraftList.DataBind();
        }

        #endregion
    }
}